package com.ziyun.pop.modules.storeMaterials.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyun.pop.modules.erpStockLog.entity.ErpStockLogEntity;
import com.ziyun.pop.modules.erpStockLog.service.ErpStockLogService;
import com.ziyun.pop.modules.purchase.entity.StorePurchaseApplyDetailEntity;
import com.ziyun.pop.modules.purchase.entity.StorePurchaseApplyEntity;
import com.ziyun.pop.modules.purchase.entity.dto.StorePurchaseApplyDto;
import com.ziyun.pop.modules.storeMaterials.dao.StorePurchaseApplyDetailDao;
import com.ziyun.pop.modules.storeMaterials.service.StorePurchaseApplyDetailService;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ZhaoWei
 * @Date 2018/10/15 18:37
 * @Version V1.0
 */
@Service("storePurchaseApplyDetailService")
public class StorePurchaseApplyDetailServiceImpl extends ServiceImpl<StorePurchaseApplyDetailDao, StorePurchaseApplyDetailEntity> implements StorePurchaseApplyDetailService {

	@Autowired
	private ErpStockLogService erpStockLogService;

	@Override
	public void saveStoreBFDate(Map<String, Object> map) {
		baseMapper.saveStoreBFDatePur(map);
	}

	@Override
	public int getStatusByApplyNo(String applyNo) {
		return baseMapper.getStatusByApplyNo(applyNo);
	}

	@Override
	public List<StorePurchaseApplyDetailEntity> getMaterialByinNo(String inNo) {
		return baseMapper.getMaterialByinNo(inNo);
	}

	@Override
	public List<StorePurchaseApplyDetailEntity> getMaterialByApplyNo(String applyNo) {
		return baseMapper.getMaterialByApplyNo(applyNo);
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public R updateDetail(StorePurchaseApplyDto storePurchaseApplyDto) {
		String applyNoNew = null;
		List<StorePurchaseApplyDetailEntity> storePurchaseApplyDetailEntityList = storePurchaseApplyDto.getStorePurchaseApplyDetailEntityList();
		// 声明一个list 集合用来存储materialID
		List<String> list = new ArrayList<>();
		for (StorePurchaseApplyDetailEntity storePurchaseApplyDetailEntity : storePurchaseApplyDetailEntityList) {
			list.add(storePurchaseApplyDetailEntity.getMaterialId());
		}
		for (int i = 0; i < 1; i++) {
			applyNoNew = storePurchaseApplyDetailEntityList.get(i).getApplyNo();
		}
		StorePurchaseApplyEntity storePurchaseApplyEntity = storePurchaseApplyDto.getStorePurchaseApplyEntity();
		Integer applyId = baseMapper.getapplyId(applyNoNew);
		Map<String, Object> map = new HashMap<>();
		map.put("applyId", applyId);
		map.put("totalMoney", storePurchaseApplyEntity.getTotalMoney());
		map.put("tax", storePurchaseApplyEntity.getTax());
		map.put("sellerId", storePurchaseApplyEntity.getSellerId());
		map.put("applyRemark", storePurchaseApplyEntity.getApplyRemark());
		map.put("createName", storePurchaseApplyEntity.getCreateName());
		Integer i = baseMapper.updateStoreapply(map);
		if (i != 1) {
			throw new RuntimeException();
		}
		//  这里是通过applyNo 查询当前数据库已经存在的
		String applyNo = storePurchaseApplyDetailEntityList.get(0).getApplyNo();
		List<StorePurchaseApplyDetailEntity> storePurchaseApplyDetailEntityList1 = baseMapper.selectApplyNo(applyNo);
		for (StorePurchaseApplyDetailEntity storePurchaseApplyDetailEntity3 : storePurchaseApplyDetailEntityList1) {
			String materialId = storePurchaseApplyDetailEntity3.getMaterialId();
			for (StorePurchaseApplyDetailEntity storePurchaseApplyDetailEntity : storePurchaseApplyDetailEntityList) {
				if (storePurchaseApplyDetailEntity.getApplyNo() == null) {
					storePurchaseApplyDetailEntity.setApplyNo(applyNoNew);
					storePurchaseApplyDetailEntity.setApplyId(applyId);
					try {
						baseMapper.insert(storePurchaseApplyDetailEntity);
					} catch (Exception e) {
						throw new RuntimeException();
					}
				} else if (storePurchaseApplyDetailEntity.getApplyNo() != null) {
					try {
						baseMapper.updateById(storePurchaseApplyDetailEntity);
					} catch (Exception e) {
						throw new RuntimeException();
					}
				}
				if (storePurchaseApplyDetailEntity.getApplyNo() != null && !list.contains(storePurchaseApplyDetailEntity3.getMaterialId())) {
					try {
						baseMapper.deleteApplyNoAndMaterialId(storePurchaseApplyDetailEntity3.getApplyNo(), materialId);
					} catch (Exception e) {
						throw new RuntimeException();
					}
				}
			}
		}
		ErpStockLogEntity erpStockLogEntity = new ErpStockLogEntity();
		erpStockLogEntity.setBillNo(applyNo);
		erpStockLogEntity.setType(1);
		erpStockLogEntity.setCreateName(storePurchaseApplyEntity.getCreateName());
		erpStockLogEntity.setLogText("采购申请修改");
		erpStockLogService.saveBatch(erpStockLogEntity);
		return R.ok();
	}

	@Override
	public BigDecimal getReceiveQuantity(String applyNO, String materialID) {
		return baseMapper.getReceiveQuantity(applyNO,materialID);
	}

	@Override
	public BigDecimal getMaterialQuantity(String applyNO, String materialID) {
		return baseMapper.getMaterialQuantity(applyNO,materialID);
	}

	@Override
	public List<StorePurchaseApplyDetailEntity> getStorePurchaseApplyDetailByApplyNo(String applyNo) {
		return baseMapper.getStorePurchaseApplyDetailByApplyNo(applyNo);
	}

	@Override
	public Integer updateReceiveQuantity(BigDecimal quantityRecive, String applyNO, String materialID) {
		return baseMapper.updateReceiveQuantity(quantityRecive,applyNO,materialID);
	}

	@Override
	public BigDecimal getTotalMoney(String inNo) {
		return baseMapper.getTotalMoney(inNo);
	}
}